package com.github.chirspan.xaas.minio.service;

import com.github.chirspan.xaas.minio.config.MinioConfigurationProperties;
import com.github.chirspan.xaas.minio.exception.SCMinioException;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * *******description*******
 * <p>
 * *************************
 *
 * @author chenpan
 * @date 2020/6/3 16:28
 */
@Component
public class MinioService {

    @Autowired(required = false)
    private MinioClient minioClient;

    @Autowired
    private MinioConfigurationProperties configurationProperties;

    public InputStream get(String fileName) throws SCMinioException {
        try {
            return minioClient.getObject(configurationProperties.getBucket(), fileName);
        } catch (XmlParserException | InvalidBucketNameException | NoSuchAlgorithmException
                | InsufficientDataException | IOException | InvalidKeyException | ErrorResponseException
                | InternalException | InvalidResponseException e) {
            throw new SCMinioException("获取文件失败" + e.getMessage(), e);
        }
    }

    public void upload(String fileName, InputStream file) throws SCMinioException {
        try {
            minioClient.putObject(configurationProperties.getBucket(), fileName, file, new PutObjectOptions(file.available(), -1));
        } catch (XmlParserException | InvalidBucketNameException | NoSuchAlgorithmException
                | InsufficientDataException | IOException | InvalidKeyException
                | ErrorResponseException | InternalException | InvalidResponseException e) {
            throw new SCMinioException("上传失败" + e.getMessage(), e);
        }
    }

    public void upload(String fileName, InputStream file, String contentType) throws SCMinioException {
        try {
            PutObjectOptions options = new PutObjectOptions(file.available(), -1);
            options.setContentType(contentType);
            minioClient.putObject(configurationProperties.getBucket(), fileName, file, options);
        } catch (NoSuchAlgorithmException | IOException | InvalidKeyException
                | InvalidResponseException | InsufficientDataException | ErrorResponseException | XmlParserException
                | InvalidBucketNameException | InternalException e) {
            throw new SCMinioException("上传失败" + e.getMessage(), e);
        }
    }

    public String getObjectUrl(String fileName) {
        try {
            return minioClient.getObjectUrl(configurationProperties.getBucket(), fileName);
        } catch (Exception e) {
            return null;
        }

    }

    public void remove(String fileName) throws SCMinioException {
        try {
            minioClient.removeObject(configurationProperties.getBucket(), fileName);
        } catch (ErrorResponseException | InsufficientDataException | InternalException
                | InvalidBucketNameException | InvalidKeyException | InvalidResponseException
                | IOException | NoSuchAlgorithmException | XmlParserException e) {
            throw new SCMinioException("删除文件失败" + e.getMessage(), e);
        }
    }
}
